﻿using System.Linq;
using AutoMapper;
using API.Helpers;
using API.IRepositories;
using API.IServices;
using API.Models;
using API.Profiles;
using AutoMapper.QueryableExtensions;
using Helper.Exceptions;
using LinqKit;

namespace API.Services
{
    public class FoodService : EndUserProfileService, IFoodService
    {
        private readonly IFoodRepository _drinkRepository;
        private readonly bool _validateCustomService;

        public FoodService()
            : this(DependencyInjectionHelper.Resolve<IEndUserProfile>(), DependencyInjectionHelper.Resolve<IFoodRepository>())
        {
        }

        public FoodService(IEndUserProfile authenticatedUserProfile, IFoodRepository drinkRepository, bool validateCustomService = true)
            : base(authenticatedUserProfile)
        {
            _drinkRepository = drinkRepository;
            _validateCustomService = validateCustomService;
        }


        public void Delete(int id)
        {
            var model = Get(id);
            if (model == null)
                throw new ValidationException("The requested Food was not found and could not be deleted.");

            _drinkRepository.Delete(id);
        }

        public IQueryable<Models.Food> Get()
        {
            return _drinkRepository
                .Get()
                .AsExpandable()
                .OrderBy(e => e.Name)
                .Project()
                .To<Models.Food>();
        }

        public Models.Food Get(int id)
        {
            return this.Get().FirstOrDefault(c => c.Id == id);
        }



        public Models.Food Save(Models.Food entity)
        {
            // ValidationHelper.ValidateModel(entity, _validateCustomService);

            return SaveSupplier(entity);
        }

        private Food SaveSupplier(Food entity)
        {
            return Mapper.Map<EntityModels.Food, Models.Food>(_drinkRepository.Save(Mapper.Map<Models.Food, EntityModels.Food>(entity), typeof(Models.Food)));
        }
    }
}

